package com.lnking.lnkingplace.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.mapper.IsolationAssistanceMapper;
import com.lnking.lnkingplace.model.dto.isolation.assistance.AssistanceDetailDto;
import com.lnking.lnkingplace.model.dto.isolation.assistance.AssistanceImportDto;
import com.lnking.lnkingplace.model.entity.EpidemicPrevention;
import com.lnking.lnkingplace.model.entity.IsolationAssistance;
import com.lnking.lnkingplace.model.entity.IsolationPersonnel;
import com.lnking.lnkingplace.model.enums.DateFormat;
import com.lnking.lnkingplace.model.request.isolation.assistance.IsolationAssistanceAddRequest;
import com.lnking.lnkingplace.model.request.isolation.assistance.IsolationAssistanceUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationAssistanceService;
import com.lnking.lnkingplace.service.IIsolationPersonnelService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 重点人员帮扶 服务实现类
 * </p>
 *
 * @author sn
 * @since 2022-08-30
 */
@Service
public class IsolationAssistanceServiceImpl extends CustomServiceImpl<IsolationAssistanceMapper, IsolationAssistance> implements IIsolationAssistanceService {
    /**
     * 删除标记
     */
    public static final String DEL="1";
    @Autowired
    IIsolationPersonnelService personnelService;
    @Override
    public void add(IsolationAssistanceAddRequest params) {
        IsolationAssistance isolationAssistance=new IsolationAssistance();
        BeanUtils.copyProperties(params,isolationAssistance);
        isolationAssistance.setCreateTime(LocalDateTime.now());
        getBaseMapper().insert(isolationAssistance);
    }

    @Override
    public void update(IsolationAssistanceUpdateRequest params) {
        IsolationAssistance isolationAssistance=new IsolationAssistance();
        BeanUtils.copyProperties(params,isolationAssistance);
        getBaseMapper().updateById(isolationAssistance);
    }

    @Override
    public AssistanceDetailDto getUserDetailById(String id) {
        return getBaseMapper().getDetailById(id);
    }

    @Override
    public void importExcel(MultipartFile file) throws Exception {
        // 先删除所有数据
        List<IsolationAssistance>  isolationAssistanceList = list();
        isolationAssistanceList.forEach(e->e.setDelFlag(DEL));
        this.updateBatchById(isolationAssistanceList);

        ImportOption option = new ImportOption.Builder().build();
        List<AssistanceImportDto> result =  ImportUtils.importExcel(file, AssistanceImportDto.class,option);;
      List<IsolationAssistance> list= new ArrayList<>();
        List<AssistanceImportDto> collect = result.stream().filter(e -> e.getPhone() != null&&!"".equals(e.getPhone())).collect(Collectors.toList());
        collect.forEach(e->{
            IsolationAssistance isolationAssistance = new IsolationAssistance();
            BeanUtils.copyProperties(e,isolationAssistance);
            // 时间转换
            isolationAssistance.setStartDate(LocalDateTimeUtil.stringToLocalDate(e.getStartDate(), DateFormat.LOCAL_DATE));
            isolationAssistance.setEndDate(LocalDateTimeUtil.stringToLocalDate(e.getEndDate(), DateFormat.LOCAL_DATE));
            isolationAssistance.setHandlingTime(LocalDateTimeUtil.stringToLocalDateTime(e.getHandlingTime(), DateFormat.LOCAL_DATE));
            isolationAssistance.setCreateTime(LocalDateTime.now());
            list.add(isolationAssistance);
        });
        //批量插入
        this.saveBatch(list);
    }

}
